//
//  HomeTabBarController.swift
//  clreader
//
//  Created by colin on 2019/2/26.
//  Copyright © 2019 colin. All rights reserved.
//

import UIKit
import Then

class HomeTabBarController: UITabBarController {

    private lazy var customTabBar = TabBar(frame: CGRect.zero)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.do {
            // 首页
            let home = HomeViewController().then {
                let item = UITabBarItem(title: "首页", image: UIImage(named: "tab_home_normal")?.withRenderingMode(.alwaysOriginal), selectedImage: UIImage(named: "tab_home_selected")?.withRenderingMode(.alwaysOriginal))
                $0.tabBarItem = item
            }
            
            // 发现
            let discovery = DiscoveryViewController().then {
                let item = UITabBarItem(title: "发现", image: UIImage(named: "tab_dis_normal")?.withRenderingMode(.alwaysOriginal), selectedImage: UIImage(named: "tab_dis_selected")?.withRenderingMode(.alwaysOriginal))
                $0.tabBarItem = item
            }
            
            //已购
            let buyed = BuyedViewController().then {
                let item = UITabBarItem(title: "已购", image: UIImage(named: "tab_buyed_normal")?.withRenderingMode(.alwaysOriginal), selectedImage: UIImage(named: "tab_buyed_selected")?.withRenderingMode(.alwaysOriginal))
                $0.tabBarItem = item
            }
            
            // 我的
            let my = MyViewController().then {
                let item = UITabBarItem(title: "我的", image: UIImage(named: "tab_my_normal")?.withRenderingMode(.alwaysOriginal), selectedImage: UIImage(named: "tab_my_selected")?.withRenderingMode(.alwaysOriginal))
                $0.tabBarItem = item
            }
            
            let controllers = [home,discovery,buyed,my]
            for con in controllers {
                con.tabBarItem?.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor(hex:"828282")], for: .normal)
                con.tabBarItem?.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor(hex:"343434")], for: .selected)
            }
            $0.viewControllers = controllers
 
            $0.setValue(self.customTabBar, forKey: "tabBar")
        }
        
    }
    
}

extension HomeTabBarController {
    
    class PlayView : UIView {
        
        lazy var buttonPlay = UIButton(type: .custom)
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            self.backgroundColor = UIColor.white
            self.layer.cornerRadius = 33.0
            self.layer.masksToBounds = true
            
            //设置按钮的位置
            self.buttonPlay.do {
                $0.layer.cornerRadius = 23.5
                $0.layer.masksToBounds = true
                $0.setImage(UIImage(named: "button_voice"), for: .normal)
                $0.addTarget(self, action: #selector(self.actionPlay(_:)), for: .touchUpInside)
            }
            self.addSubview(self.buttonPlay)
            self.buttonPlay.snp.makeConstraints { (make) in
                make.size.equalTo(CGSize(width: 47.0, height: 47.0))
                make.center.equalToSuperview()
            }
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        @objc private func actionPlay(_ sender:Any) {
            let controller = AudioPlayViewController()
            controller.hidesBottomBarWhenPushed = true
            UIApplication.shared.keyWindow?.rootViewController?.show(controller, sender: nil)
        }
    }
    
    class TabBar: UITabBar {
        
        lazy var playStatusView = PlayView(frame: CGRect.zero)
        
        private let playStatusViewSize = CGSize(width: 66.0, height: 66.0)
        
        override init(frame: CGRect) {
            super.init(frame: frame)
            self.addSubview(self.playStatusView)
            self.playStatusView.snp.makeConstraints { (make) in
                make.size.equalTo(self.playStatusViewSize)
                make.centerX.equalToSuperview()
                make.top.equalToSuperview().offset(-18.0)
            }
            
            self.backgroundImage = UIImage()
            self.shadowImage = UIImage()
        }
        
        override func layoutSubviews() {
            super.layoutSubviews()

            //去掉TabBar上部的横线
            for subview in self.subviews {
                if subview is UIImageView && subview.bounds.height <= 1.0 { //横线的高度为0.5
                    subview.isHidden = true
                }
            }
            
            let buttonWidth = (self.bounds.width - self.playStatusViewSize.width) / 4
            
            var index:CGFloat = 0
            var x:CGFloat = 0
            
            for subview in self.subviews {
                if subview.isKind(of: NSClassFromString("UITabBarButton")!) {
                    subview.frame = CGRect(x: x, y: 0, width: buttonWidth, height: subview.bounds.height)
                    x += buttonWidth
                    if index == 1 { x += self.playStatusViewSize.width }
                    index += 1
                }
            }
            self.bringSubviewToFront(self.playStatusView)
        }
        
        override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
            if !self.isHidden {
                let location = self.convert(point, to: self.playStatusView)
                if self.playStatusView.point(inside: location, with: event) {
                    return self.playStatusView.buttonPlay
                } else {
                    return super.hitTest(point, with: event)
                }
            } else {
                return super.hitTest(point, with: event)
            }
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
    }
}
